#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size(), len = 0;
        int left = 0, right = 0;
        int countZero = 0;
        while (right < n)
        {
            if (nums[right] == 0)
                countZero++;
            while (countZero == k + 1)
            {
                if (nums[left] == 0)
                    countZero--;
                left++;
            }
            len = max(len, right - left + 1);
            right++;
        }
        return len;
    }
};